package com.yanqu.road.server.manager.tomb;

import com.yanqu.road.server.manager.tomb.business.TombCrossBusiness;
import com.yanqu.road.server.manager.tomb.entity.DbTombLog;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TombLog {

    private TombActivity tombActivity;

    private List<DbTombLog> waitList = new ArrayList<>();
    private final Lock lock = new ReentrantLock();

    public static TombLog newInstance(TombActivity activityInfo) {
        TombLog tombLog = new TombLog();
        tombLog.tombActivity = activityInfo;
        return tombLog;
    }

    private final Map<String, List<DbTombLog>> dbTombLogListMap = new ConcurrentHashMap<>();


    public synchronized void addDbTombLog(DbTombLog dbTombLog, boolean isAll) {
        int limit = 100;
        if (isAll) {
            limit = this.tombActivity.getConfig().getConfig().getTOMB_LOG_LIMIT();
        } else {
            limit = this.tombActivity.getConfig().getConfig().getTOMB_PERSONAL_LOG_LIMIT();
        }
        dbTombLog.setActivityId(this.tombActivity.getActivityInfo().getActivityId());
        if (!this.dbTombLogListMap.containsKey(dbTombLog.getUid())) {
            this.dbTombLogListMap.put(dbTombLog.getUid(), new ArrayList<>());
            Map<Long, DbTombLog> dbTombLogMap = TombCrossBusiness.getDbTombLog(this.tombActivity.getActivityInfo().getActivityId(), dbTombLog.getUid(), limit);
            this.dbTombLogListMap.get(dbTombLog.getUid()).addAll(dbTombLogMap.values());
        }
        List<DbTombLog> dbTombLogs = this.dbTombLogListMap.get(dbTombLog.getUid());
        dbTombLogs.add(dbTombLog);
        if (dbTombLogs.size() > limit) {
            dbTombLogs.remove(0);
        }
        dbTombLog.setNeedInsert(true);
        dbTombLog.refreshUpdateTime();

        this.waitList.add(dbTombLog);
    }


    public synchronized List<DbTombLog> getList(String uid, boolean isAll) {
        List<DbTombLog> dbTombLogs = this.dbTombLogListMap.get(uid);
        if (dbTombLogs != null) {
            ArrayList<DbTombLog> list = new ArrayList<>(dbTombLogs);
            return list;
        }
        this.dbTombLogListMap.put(uid, new ArrayList<>());
        int limit = 0;
        if (isAll) {
            limit = this.tombActivity.getConfig().getConfig().getTOMB_LOG_LIMIT();
        } else {
            limit = this.tombActivity.getConfig().getConfig().getTOMB_PERSONAL_LOG_LIMIT();
        }
        Map<Long, DbTombLog> dbTombLogMap = TombCrossBusiness.getDbTombLog(this.tombActivity.getActivityInfo().getActivityId(), uid, limit);
        this.dbTombLogListMap.get(uid).addAll(dbTombLogMap.values());
        List<DbTombLog> list = new ArrayList<>(this.dbTombLogListMap.get(uid));
        list.sort(Comparator.comparing(DbTombLog::getCreateTime).reversed());
        return list;
    }

    public synchronized List<DbTombLog> getUserList(long userId, String unionUid) {
        List<DbTombLog> list = new ArrayList<>();
        List<DbTombLog> list1 = this.getList(String.valueOf(userId), true);
        List<DbTombLog> list2 = this.getList(unionUid, true);
        list.addAll(list1);
        list.addAll(list2);
        list.sort(Comparator.comparing(DbTombLog::getCreateTime).reversed());
        int limit = this.tombActivity.getConfig().getConfig().getTOMB_PERSONAL_LOG_LIMIT();
        List<DbTombLog> resultList = new ArrayList<>();
        for (DbTombLog dbTombLog : list) {
            resultList.add(dbTombLog);
            if (resultList.size() >= limit) {
                break;
            }
        }
        return resultList;
    }

    public synchronized List<DbTombLog> getWaitList() {
        List<DbTombLog> list = new ArrayList<>();
        for (DbTombLog dbTombLog : this.waitList) {
            list.add(dbTombLog);
        }
        this.waitList.clear();
        return list;
    }

}
